package cyclequeue;

import queue.NullQueueException;

//使用usedSize记录队列中有效位数，来实现双端队列
public class MyQueue1 {
    private int[] elem;
    private int usedSize;
    private int front;
    private int rear;

    public MyQueue1() {
        this.elem = new int[10];
        this.usedSize = 0;
        this.front = 0;
        this.rear = 0;
    }
    //从rear下标位置入队
    public boolean offer(int val) {
        if(isFull()) {
            return false;
        }
        elem[rear] = val;
        //rear++; error
        rear = (rear +1) % elem.length;
        usedSize++;
        return true;
    }
    //从front下标位置出队
    public boolean poll() {
        if(isEmpty()) {
            return false;
        }
        front = (front + 1) % elem.length;
        usedSize--;
        return true;
    }

    public int peekFront() {
        return elem[front];
    }

    public int peekRear() {
        return rear == 0 ? elem[elem.length-1] : elem[rear-1];
    }
    public int size() {
        return usedSize;
    }

    public boolean isEmpty() {
        return rear == front;
    }

    public boolean isFull() {
        return usedSize == elem.length;
    }
}
